library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.1
## ✓ tidyr   1.1.1     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(units)
## udunits system database from /Users/Aine/Library/R/4.0/library/units/share/udunits
library(USAboundaries)
library(USAboundariesData)
library(rnaturalearth)
library(gghighlight)
library(ggrepel)
library(knitr)
library(rmapshaper)
## Registered S3 method overwritten by 'geojsonlint':
##   method         from 
##   print.location dplyr
library(leaflet)
library(readxl)

##question 1
conus = USAboundaries::us_states() %>%
 st_transform(5070) %>% 
 filter(!state_name %in% c("Puerto Rico", "Alaska", "Hawaii"))

conus_simp = ms_simplify(conus, keep = 0.05)
 
get_conus = function(data, var){
  filter(data, !get(var) %in%
           c("Hawaii", "Puerto Rico", "Alaska"))
}
counties = st_transform(us_counties(), 5070) %>%
  get_conus("state_name") %>% 
  st_as_sf()
 
county_centroid = st_centroid(counties) %>%
  st_combine() %>%
  st_cast("MULTIPOINT")
## Warning in st_centroid.sf(counties): st_centroid assumes attributes are constant
## over geometries of x
# voroni 
vgrid = st_voronoi(county_centroid) %>%
  st_cast() %>%
  st_as_sf %>%
  mutate(id = 1:n())
# triangulated 
tgrid = st_triangulate(county_centroid) %>%
  st_cast() %>%
  st_as_sf() %>%
  mutate(id = 1:n())
# gridded, n = 70
sqgrid = st_make_grid(conus_simp, n = c(70, 50)) %>%
  st_as_sf() %>%
  st_cast() %>%
  mutate(id = 1:n())
# hexagonal, n = 70
hexgrid = st_make_grid(conus_simp, n = c(70, 50), square = FALSE) %>%
  st_as_sf() %>%
  st_cast() %>%
  mutate(id = 1:n())

plot_tess = function(data, title)
  {ggplot() + 
    geom_sf(data = data, fill = "white", col = "navy", size = .2) +   
    theme_void() +
    labs(title = title, caption = paste("This tesselation has:", nrow(data), "tiles" )) +
    theme(plot.title = element_text(hjust = .5, color =  "Turquoise", face = "bold"))}
# Original
plot_tess(data = counties, "Original County Data")

# Voroni
vgrid = st_intersection(vgrid, st_union(conus_simp))
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
plot_tess(vgrid, "Voronoi Coverage") +
  geom_sf(data = county_centroid, col = "navy", size = 0.2)

# Triangulated
tgrid = st_intersection(tgrid, st_union(conus_simp))
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
plot_tess(tgrid, "Triangulated Coverage") +
  geom_sf(data = county_centroid, col = "navy", size = 0.2)

# Gridded
plot_tess(sqgrid, "Square Coverage")

# Hexagonal
plot_tess(hexgrid, "Hexagonal Coverage")

Tessellation Characteristics
Type Elements Mean Area (km2) Standard Deviation Area (km2) Coverage Area
Counties 3,108 2,521.745 3,404.325 7,837,583
Voroni 3,106 2,521.817 2,886.871 7,832,763
Triangulation 6,195 1,251.902 1,578.183 7,755,535
Grid 2,252 3,761.443 0.000 8,470,770
Hexagon 2,247 3,759.592 0.000 8,447,802

I believe that either the grid or hexagons would be the most likely to show accuracy and reliability as they are all equal area. For example, the Voroni has a standard deviation of over 100,000, meaning that there is a relatively significant difference between the sizes of tiles. The larger a tile is, the more likely it is to have more points in them, by simply understanding the basic laws of probability. I have decided to use the grid as it has a slightly larger number of elements and coverage area, as compared to its closest second, the hexagons.

#### The first type of dam I chose was recreation, as I was curious about its locations for usage, as I would have assumed most types of dams would not be used for something as trivial as recreation. I next chose water supply, as to me, that seems like the most common use for a dam, although that was not true by the above table, so I was curious as to where these would be congregated. The last two I chose were flood control and irrigation as I wanted to see how these two compared with one another.

#### The most dams for recreational purposes seemed to be concentrated on the southern east coast, which I found surprising, but also seemed to make sense as that would be a very warm part of the country, especially during summer months, where recreational use to cool down would relatively popular. Concentrations of dams for water supply seem to be relatively spread out, which makes sense as water supply would be necessary for all populations. I think the flood control makes sense as it is concentrated in areas with a lot of rivers. I also think the irrigation makes sense as it is mostly in farmland. I feel like the grid system is relatively accurate, as it displays information with a uniform unit of size.